import Vue from 'vue'
import Router from 'vue-router'
import index from '@/pages/index/index.vue'
import login from '@/pages/index/login'
import register from '@/pages/index/register'
import forgetPassword from '@/pages/index/forgetPassword'
import userHome from '@/pages/index/userHome'
import personMessage from '@/pages/index/personMessage'
import joinTest from '@/pages/index/joinTest'
import test1 from '@/pages/index/test1'
import test2 from '@/pages/index/test2'
import print1 from '@/pages/index/print1'
import print2 from '@/pages/index/print2'
import score from '@/pages/index/score'
import count from '@/pages/index/count'
import progress from '@/pages/index/progress'
import progressDetail from '@/pages/index/progressDetail'
import notice from '@/pages/index/notice'
import noticeDetail from '@/pages/index/noticeDetail'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/',
      name: '首页',
      component: index,
      //redirect:'/userHome/personMessage',
    },
    {
      path: '/notice',
      name: '通知',
      component: notice,
      //redirect:'/userHome/personMessage',
    },
    {
      path: '/noticeDetail',
      name: '通知',
      component: noticeDetail,
      //redirect:'/userHome/personMessage',
    },
    {
      path: '/login',
      name: '登录',
      component: login
    },
    {
      path: '/register',
      name: '注册',
      component: register
    },
    {
      path: '/forgetPassword',
      name: '找回密码',
      component: forgetPassword
    },
    {
      path: '/userHome',
      name: 'userHome',
      component: userHome,
      redirect:'/userHome/personMessage',
      children:[
          {path:'/userHome/personMessage',component:personMessage,name:'基础信息',show:true,iconCls: 'el-icon-menu'},
          {path:'/userHome/joinTest',component:joinTest,name:'我要报考',show:true,iconCls: 'el-icon-edit-outline'},
          {path:'/userHome/test1',component:test1},
          {path:'/userHome/test2',component:test2},
          {path:'/userHome/progress',component:progress,name:'进度查询',show:true,iconCls: 'el-icon-time'},
          {path:'/userHome/print1',component:print1,name:'下载表格',show:true,iconCls:'el-icon-printer'},
          {path:'/userHome/print2',component:print2},
          {path:'/userHome/score',component:score,name:'成绩查询',show:true,iconCls: 'el-icon-search'},
          {path:'/userHome/count',component:count,name:'账户管理',show:true,iconCls: 'el-icon-setting'},
          {path:'/userHome/progressDetail',component:progressDetail}
      ]
    }

  ]
})

router.beforeEach((to, from, next) => {
  console.log('to:' + to.path)
  console.log('from:' + from.path)
    if (to.path.startsWith('/login')) {
      window.sessionStorage.removeItem('access-user')
      next()
    } 
    else {
      if(to.path.startsWith('/userHome')){
        let user = JSON.parse(window.sessionStorage.getItem('access-user'))
        if (!user) {
          next({path: '/login'})
        } else {
          next()
        }
      }else{
        next()
      }
    }
})

export default router